﻿@using Signum.Entities.DynamicQuery
@using Signum.Entities.Reflection
@using Signum.Web.Properties
@using Signum.Engine
@model Context

@{ 
   QueryDescription queryDescription = (QueryDescription)ViewData[ViewDataKeys.QueryDescription];
   var entityColumn = queryDescription.Columns.SingleEx(a => a.IsEntity);
   Type entitiesType = Lite.Extract(entityColumn.Type);
   Implementations implementations = entityColumn.Implementations;
   bool viewable = (bool)ViewData[ViewDataKeys.View] && (implementations != null || Navigator.IsViewable(entitiesType, EntitySettingsContext.Admin));
   bool? allowMultiple = (bool?)ViewData[ViewDataKeys.AllowMultiple];

   FilterMode filterMode = (FilterMode)ViewData[ViewDataKeys.FilterMode];
   
   ResultTable queryResult = (ResultTable)ViewData[ViewDataKeys.Results];
   Dictionary<int, Func<HtmlHelper, object, MvcHtmlString>> formatters = (Dictionary<int, Func<HtmlHelper, object, MvcHtmlString>>)ViewData[ViewDataKeys.Formatters];

   int columnsCount = queryResult.Columns.Count() + (viewable ? 1 : 0) + (allowMultiple.HasValue ? 1 : 0);
}

@if (ViewData.ContainsKey(ViewDataKeys.MultipliedMessage))
{ 
    <tr class="sf-tr-multiply">
        <td class="sf-td-multiply ui-state-highlight" colspan="@columnsCount">
            <span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>
            @ViewData[ViewDataKeys.MultipliedMessage]
        </td>
    </tr>
}

@foreach (var row in queryResult.Rows)
{
    Lite entityField = row.Entity;
    <tr data-entity="@entityField.Key()">
        @if (allowMultiple.HasValue)
        {
            <td>
                @if (allowMultiple.Value)
                {
                    @Html.CheckBox(
                        Model.Compose("rowSelection", row.Index.ToString()),
                          new { value = entityField.Id.ToString() + "__" + Navigator.ResolveWebTypeName(entityField.RuntimeType) + "__" + entityField.ToString() });

                }
                else
                {
                    @Html.RadioButton(
                             Model.Compose("rowSelection"),
                        entityField.Id.ToString() + "__" + Navigator.ResolveWebTypeName(entityField.RuntimeType) + "__" + entityField.ToString());
                }
            </td>
        }
        @if (viewable)
        {
            <td>
                @QuerySettings.EntityFormatRules.Last(fr => fr.IsApplyable(entityField)).Formatter(Html, entityField)
            </td>
        }
        @foreach (var col in queryResult.Columns)
        {
            <td>
                @formatters[col.Index](Html, row[col])
            </td>
        }
    </tr>
}

@if (queryResult.Rows.IsNullOrEmpty())
{
    <tr>
        <td colspan="@columnsCount">@Resources.Signum_noResults</td>
    </tr>
}

@{ 
    ViewData[ViewDataKeys.ElementsPerPage] = queryResult.ElementsPerPage;
    ViewData[ViewDataKeys.SearchControlColumnsCount] = columnsCount;
}
@Html.Partial(Navigator.Manager.PaginationView, Model)